<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = !showDialog">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              size="mini"
              active-color="#13ce66"
              inactive-color="#dcdfe6"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else style="margin-left: 10px">{{ row.state === 1 ? "已启用" : "未启用" }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="{ row }">
            <div v-if="row.isEdit">
              <el-button type="text" size="small" @click="add(row)">确定</el-button>
              <el-button type="text" size="small" @click="row.isEdit = false">取消</el-button>
            </div>

            <div v-else>
              <el-button size="mini" type="text" @click="show(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="row.isEdit = true">编辑</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">

        <!-- 放置分页组件 -->
        <el-pagination
          :page-sizes="[5, 6,8,10]"
          :current-page="page"
          layout=" prev, pager, next ,sizes"
          :total="total"
          :page-size="pagesize"
          @current-change="hCurrentChange"
          @size-change="hSizeChange"
        />
      </el-row>
    </div>
    <addDept :show-dialog="showDialog" @updateOn="showDialog = false" />
    <assign-prem ref="getPrem" :cur-id="curId" :show-ass.sync="showAss" />
  </div>
</template>
<script>
import { getRoleList, delRoleList, updataRole } from '@/api/role'
import addDept from './components/addDept.vue'
import AssignPrem from './components/assignPrem.vue'

export default {
  name: 'Role',
  components: { addDept, AssignPrem },
  data() {
    return {
      curId: -1,
      list: [],
      total: 0,
      page: 1,
      pagesize: 5,
      showAss: false,
      showDialog: false,
      isEdit: false
    }
  },
  computed: {

  },
  created() {
    this.getRole()
  },
  methods: {
    show(id) {
      this.curId = id
      this.showAss = true
      this.$nextTick(() => {
        this.$refs.getPrem.getPrem()
      })
    },
    async add(row) {
      // console.log(row)
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.success('内容不能为空')
      } else {
        await updataRole(row.editRow)
        this.getRole()
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
        // eslint-disable-next-line require-atomic-updates
        row.isEdit = false
      }
    },
    hSizeChange(pages) {
      this.pagesize = pages
      this.page = 1
      this.getRole()
    },
    hCurrentChange(page) {
      this.page = page
      this.getRole()
    },
    async getRole() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      // console.log(res.data)
      this.list = res.data.rows
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
      this.total = res.data.total
    },

    del(id) {
      this.$confirm('确认删除？',
        { confirmButtonText: '确定',
          cancelButtonText: '取消' })
        .then(async _ => {
          // console.log(id)
          await delRoleList(id)
          if (this.list.length === 1) {
            if (this.page > 1) {
              this.page--
            }
          }
          this.getRole()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(_ => {})
    }
  }

}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
